<template>
  <a-row id="globalHeader" align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="selectedKeys"
        @menu-item-click="doMenuClick"
      >
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="title-bar">
            <img
              :src="require('../../public/log.jpg')"
              alt="logo"
              class="w-8 h-8 mr-2"
              style="border-radius: 50%; opacity: 0.5"
            />
            <div class="title">算法分析平台</div>
          </div>
        </a-menu-item>
        <a-menu-item v-for="item in visibleRoutes" :key="item.path">
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <div class="nameForLogin">
        <a-space size="large">
          <a-dropdown
            v-if="store.state.user.loginUser.userName !== '未登录'"
            style="margin-right: 10px"
          >
            <a-avatar
              :image-url="store.state.user?.loginUser?.userAvatar"
              alt="用户头像"
              style="margin-right: 10px"
            />
            <div style="margin-right: 20px">
              {{ store.state.user?.loginUser?.userName ?? "未登录" }}
            </div>
            <template #content>
              <a-doption @click="clickTo('person')">个人中心</a-doption>
              <a-doption @click="clickTo('logOut')">退出登录</a-doption>
            </template>
          </a-dropdown>
          <a-button
            v-else
            @click="userLogIn"
            style="font-size: larger; margin-right: 20px"
            >登录
          </a-button>
        </a-space>
      </div>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "@/router/routes";
import { useRouter } from "vue-router";
import { computed, nextTick, onMounted, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import { UserControllerService } from "../../xiongoj_backendapi";
import axios from "axios";

const router = useRouter();
const store = useStore();
//智能显示登录或者个人中心标签

//下拉菜单切换到其他页面
const clickTo = (pageName: string) => {
  // 根据页面名称执行导航逻辑
  if (pageName === "person") {
    router.push("/person");
  } else if (pageName === "logOut") {
    UserControllerService.userLogoutUsingPost();
    location.reload();
  }
  // 可以添加更多的页面调整逻辑
};

//如果没有登录跳转到登录界面
const userLogIn = () => {
  // 模拟登录逻辑
  router.push("/user/login");
};
// 展示在菜单的路由数组
const visibleRoutes = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    // 根据权限过滤菜单
    return checkAccess(
      store.state.user.loginUser,
      item?.meta?.access as string
    );
  });
});

// 默认主页
const selectedKeys = ref(["/"]);

// 路由跳转后，更新选中的菜单项
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

// // 模拟三秒后登录
// setTimeout(() => {
//   store.dispatch("user/getLoginUser", {
//     userName: "丁家雄",
//     userRole: accessEnum.ADMIN,
//   });
// }, 3000);

const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};
// ----------------------------处理用户头像------------------------------
const avatarUrl = ref("");
onMounted(async () => {
  avatarUrl.value = store.state.user?.loginUser?.userAvatar;
});
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: #444;
  margin-left: 16px;
}

.nameForLogin {
  font-size: larger;
  font-weight: bolder;
}

.logo {
  height: 48px;
}
</style>
